package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTopicCrtLessonController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/crt/lesson", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        YxyProfile yxyProfile = getProfile(request, response);
        String schoolId = yxyProfile.getSchoolId();
        String author = yxyProfile.getUserNick();
        String authorId = yxyProfile.getId();
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String catalogId3 = reqModel.catalogId3;
        String name = StringTrimUtil.trim(reqModel.name);
        String summary = reqModel.summary;
        String cover = (StringUtils.isNotBlank(reqModel.cover) ? reqModel.cover : "https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/136dc44072f22ac951a1e913f91488df.jpg");

        Integer orderNum = lessonRepository.countBySchoolId(schoolId) + 1;

        YxyLessonsTopicLesson yxyLessonsTopicLesson = new YxyLessonsTopicLesson();
        yxyLessonsTopicLesson.setId(UUID.randomUUID().toString().replace("-", ""));
        yxyLessonsTopicLesson.setCatalogId1(catalogId1);
        yxyLessonsTopicLesson.setCatalogId2(catalogId2);
        yxyLessonsTopicLesson.setCatalogId3(catalogId3);
        yxyLessonsTopicLesson.setName(name);
        yxyLessonsTopicLesson.setSummary(summary);
        yxyLessonsTopicLesson.setVip(0);
        yxyLessonsTopicLesson.setCount(0);
        yxyLessonsTopicLesson.setDuration(0);
        yxyLessonsTopicLesson.setCreateDate(new Date());
        yxyLessonsTopicLesson.setStatus(0);
        yxyLessonsTopicLesson.setPv(0);
        yxyLessonsTopicLesson.setVv(0);
        yxyLessonsTopicLesson.setIsVisible(0);
        yxyLessonsTopicLesson.setSchoolId(schoolId);
        yxyLessonsTopicLesson.setAuthor(author);
        yxyLessonsTopicLesson.setAuthorId(authorId);
        yxyLessonsTopicLesson.setCover(cover);
        yxyLessonsTopicLesson.setOrderNum(orderNum);
        yxyLessonsTopicLesson.setIsYx(0);
        yxyLessonsTopicLesson.setSync(0);
        yxyLessonsTopicLesson = lessonRepository.save(yxyLessonsTopicLesson);

        RespModel respModel = new RespModel();
        respModel.lessonId = yxyLessonsTopicLesson.getId();
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String catalogId1;
        public String catalogId2;
        public String catalogId3;
        public String cover;
        public String name;
        public String summary;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(catalogId1), "请选择一级分类名称");
            Assert.isTrue(StringUtils.isNotBlank(catalogId2), "请选择二级分类名称");
            Assert.isTrue(StringUtils.isNotBlank(catalogId3), "请选择三级分类名称");
            Assert.isTrue(StringUtils.isNotBlank(name), "课程名称不能为空");
            Assert.isTrue(name.length() > 0 && name.length() < 36, "请输入1-35个的课程名称");
            Assert.isTrue(StringUtils.isNotBlank(summary), "课程简介不能为空");
            Assert.isTrue(summary.length() > 0 && summary.length() < 501, "请输入1-500字的简介");
            // Assert.isTrue(StringUtils.isNotBlank(cover), "课程封面不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String lessonId;
    }
}
